package com.example.Sao.MyActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.Sao.R;
import com.example.Sao.Serivce.SerialPortService;

public class PreferenceHead extends Preference{
		private final String TAG = "PreferenceHead";
		
        private OnCheckedChangeListener mOnCheckedChangeListener;
        private android.widget.CompoundButton.OnCheckedChangeListener mOnCheckedChangeScankey;
      //  private OnClickListener   mOnClickListener;
        private Context  mContext;
        private TextView mTvTimeOut;
	 
	    public PreferenceHead(Context context) {
	        super(context);
	        this.mContext = context;
	        setLayoutResource(R.layout.preference_head);
	    }
	 
	    @Override
	    protected void onBindView(View view) {
	        super.onBindView(view);

	         RadioGroup  mRadioGroup = (RadioGroup)view.findViewById(R.id.mRadioGroupModus);
	         RadioButton mBtSig,mBtQR;
	         CheckBox mBtLeft,mBtRight;
	         mBtSig   = (RadioButton)view.findViewById(R.id.radioSig);
	         mBtQR    = (RadioButton)view.findViewById(R.id.radioQR);
	         mBtLeft  = (CheckBox)view.findViewById(R.id.radioLeft);
	         mBtRight = (CheckBox)view.findViewById(R.id.radioRight);
	         mTvTimeOut = (TextView)view.findViewById(R.id.mTvTimeOut);
	  		 SharedPreferences mSP = mContext.getSharedPreferences(MyApplication.SYMBOL_CONFIG_SIG, 0);
	  		 int mTimeOutValue     = mSP.getInt(MyApplication.SCAN_TIME_OUT, 30);
	         mTvTimeOut.setText(""+mTimeOutValue);
	         
	         Button   mTimeout   = (Button)view.findViewById(R.id.btSetTimeOut);
	         mTimeout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					showDialog();
				}
			});
	          
  	  		  boolean isSig  =  mSP.getBoolean(MyApplication.SCAN_MODE_SIG,false);

	  		  if(isSig){
	  			mBtSig.setChecked(true); 
	  		  }else{
	  			mBtQR.setChecked(true);  
	  		  }
	  		  
	  		boolean isLeftKey =  mSP.getBoolean(MyApplication.SCAN_MODE_LEFT,true);
	  		boolean isRightKey =  mSP.getBoolean(MyApplication.SCAN_MODE_RIGHT,true);
	          
	  		if(isLeftKey){
 	  			mBtLeft.setChecked(true);
	  		}
	  		
	  		if(isRightKey){
	  			mBtRight.setChecked(true);
		  	 }
	  		
			  mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					
					if( mOnCheckedChangeListener != null ){
						mOnCheckedChangeListener.onCheckedChanged(group, checkedId);
					}
				}
			 });
			  
			  mBtLeft.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if(mOnCheckedChangeScankey != null){
						mOnCheckedChangeScankey.onCheckedChanged(buttonView, isChecked);
					}
					
				}
				  
			  });
			  
			  mBtRight.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(mOnCheckedChangeScankey != null){
						mOnCheckedChangeScankey.onCheckedChanged(buttonView, isChecked);					
				   	}
					
				}
			});
			  
		   Button  mBtFactory = (Button)view.findViewById(R.id.mbtFactory);	  
		   mBtFactory.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				Intent intent = new Intent(SerialPortService.BARCODEPORT_WRITEDATA_ACTION);
				intent.putExtra(SerialPortService.BARCODEPORT_WRITEDATA_EXTRA_DATA, MyApplication.FACTORY_DEFAULT);
				getContext().sendBroadcast(intent);
			}
		  });
		   
		  TextView mProTimeOut = (TextView)view.findViewById(R.id.tvProTimeOut);
		  String mTextStr = this.getContext().getResources().getString(R.string.str_timeout_prompt);  //str_timeout_prompt
		  
		  if(isSig){
			  mProTimeOut.setText(String.format(mTextStr, 65));
		  }else{
			  mProTimeOut.setText(String.format(mTextStr, 300));
		  }
	        
	    }
	 
	    public void setOnCheckedChangeListener(OnCheckedChangeListener mListener) {
	        this.mOnCheckedChangeListener = mListener;
	    }
	    public void setOnCheckedChangeScankey(android.widget.CompoundButton.OnCheckedChangeListener mListener){
	    	this.mOnCheckedChangeScankey = mListener;
	    }
	    
	    private void showDialog() {

	  		final Dialog  mDialog = new Dialog(getContext(),R.style.dialog1);
	  		mDialog.setContentView(R.layout.timeout_dialog_view);
	  		final EditText mTimeOutValue = (EditText)mDialog.findViewById(R.id.mEdTimeoutValue);
	  		Button  mEnsure = (Button)mDialog.findViewById(R.id.mBtEnsure);
	  		Button  mCancle = (Button)mDialog.findViewById(R.id.mBtCancel);
	  		
	  		mDialog.show();
	  		
	  		mEnsure.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					String mStr = mTimeOutValue.getText().toString();
					if(!TextUtils.isEmpty(mStr)){
						
				  		SharedPreferences mSP = getContext().getSharedPreferences(MyApplication.SYMBOL_CONFIG_SIG, 0);
				  		boolean isSig  =  mSP.getBoolean(MyApplication.SCAN_MODE_SIG,false);
				  		int  mTimeOut = Integer.parseInt(mStr);
				  		
				  		if(isValueValid(isSig, mTimeOut)){

					  		 SharedPreferences.Editor editor = mSP.edit();
					  	     editor.putInt(MyApplication.SCAN_TIME_OUT, mTimeOut);
					  		 editor.commit();
					  		
					  		 mTvTimeOut.setText(mStr);
					  		 String mSetTime = "";
					  		 
					  		 if(isSig){
								 mSetTime = "aosmpt"+mTimeOut*1000+"."; 
					  		 }else{
					  			 mSetTime = "hstcdt"+mTimeOut*1000+".";
					  		 }

							Intent intentSetTime = new Intent(SerialPortService.BARCODEPORT_WRITEDATA_ACTION);
							intentSetTime.putExtra(SerialPortService.BARCODEPORT_WRITEDATA_EXTRA_DATA, mSetTime);
							getContext().sendBroadcast(intentSetTime);  //aosmpt   scndly
							
							mDialog.dismiss();
						
				  		}else{
							Toast.makeText(getContext(), R.string.str_invalid_value, Toast.LENGTH_LONG).show();
						}
					}
				}
			});
	  		
	  		mCancle.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mDialog.dismiss();
				}
			});

	  		
	  	}
	    
	    
	    private boolean isValueValid(boolean isSig, int mTimeOut){
	    	
		    	if(isSig){
		    		if(mTimeOut >= 1 &&  mTimeOut <= 65){
		    			return true;
		    		}
		    	}else{
		    		if(mTimeOut >=1 && mTimeOut <= 300 ){
		    			return true;
		    		}
		    	}
		    	
		    	return false;
	    }

}